var fs = require('fs');
const {resolve} = require('path')
var nowpath = resolve('./');
const { MyDate, DATE_FORMATTER, DATE_ENUM, DATE_PROP_TYPE } = require("myjs-common");
var set = function(obj){
	var oldobj = require(nowpath + '/session.json');
	for(var i in obj){
		if(!obj[i].end){
			obj[i].time = 'auto';
		}else{
			obj[i].time = new MyDate().add(obj[i].end, DATE_PROP_TYPE.SECONDS);
		};
		delete obj[i].end;
		oldobj[i] = obj[i];
	};
	
	fs.writeFileSync(nowpath + '/session.json', JSON.stringify(oldobj));
};
var get = function(name,type){
	var oldobj = require(nowpath + '/session.json');
	if(type){
		for(var i in oldobj){
			var obj = oldobj[i];
			if(obj.time == 'auto'){
				delete oldobj[i];
			};
		};
		fs.writeFileSync(nowpath + '/session.json', JSON.stringify(oldobj));
	};
	if(!name || !oldobj[name]) return false;
	oldobj[name].time = new MyDate(oldobj[name].time);
	// console.log(oldobj[name].time)
	if(!(oldobj[name].time).after(new Date())){
		delete oldobj[name];
		fs.writeFileSync(nowpath + '/session.json', JSON.stringify(oldobj));
		return false;
	};
	return oldobj[name].val;
};
var del = function(name){
	var oldobj = require(nowpath + '/session.json');
	if(!oldobj[name]) return;
	delete oldobj[name];
	fs.writeFileSync(nowpath + '/session.json', JSON.stringify(oldobj));
};
module.exports = {set,get,del};